﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ePioneer_Center_ep_courseinfo_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            EMPCModel.EMPCEntities dal = new EMPCModel.EMPCEntities();
            var list = dal.ePioneer_Lecturer.OrderBy(p => p.Lecturer).ToList();
            ddl_Lecturer.DataSource = list;
            ddl_Lecturer.DataBind();
            if (Request["id"] != null)
            {
                int id = int.Parse(Request["id"].ToString());

                EMPCModel.ePioneer_CourseInfo m_epci = dal.ePioneer_CourseInfo.First(x => x.Id == id);
                rbl_Line.SelectedValue = m_epci.IsLine.ToString();
                ddl_CourseType.SelectedValue = m_epci.CourseType;
                ddl_SolutionType.SelectedValue = m_epci.SolutionType;
                ddl_Lecturer.SelectedValue = m_epci.Lecturer.ToString();
                txt_BriefInfo.Text = m_epci.BriefInfo;
                txt_CourseStartDate.DateTimeValue = m_epci.CourseStartDate;
                dll_EndTime.SelectedValue = m_epci.EndTime;
                txt_IReservation.Text = m_epci.IReservation.ToString();
                txt_MainContent.Text = m_epci.MainContent;
                dll_StartTime.SelectedValue = m_epci.StartTime;
                txt_Title.Text = m_epci.Title;
                lb_AttachmentName.Text = m_epci.AttachmentName;
                txt_url.Text = m_epci.Url;
                if (m_epci.IsTop == 1)
                    checkIsTop.Checked = true;
                else
                    checkIsTop.Checked = false;
                lb_AttachmentName.Attributes.Add("OnClick", "window.open('" + m_epci.Attachment + "');return false;");
            }
        }
    }
    protected void BtnModify_Click(object sender, EventArgs e)
    {
        if (txt_Title.Text.Trim() == "")
            return;
        if (Request["id"] != null)
        {
            try
            {
                int id = int.Parse(Request["id"].ToString());
                EMPCModel.EMPCEntities dal = new EMPCModel.EMPCEntities();
                EMPCModel.ePioneer_CourseInfo m_epci = dal.ePioneer_CourseInfo.First(x => x.Id == id);
                m_epci = GetModel(m_epci);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='ep_courseinfo.aspx'", true);
            }
            catch (Exception)
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改失败');", true);
            }
        }
        else
        {
            EMPCModel.EMPCEntities dal = new EMPCModel.EMPCEntities();
            EMPCModel.ePioneer_CourseInfo m_epci = new EMPCModel.ePioneer_CourseInfo();
            m_epci = GetModel(m_epci);
            try
            {
                dal.AddToePioneer_CourseInfo(m_epci);
                dal.SaveChanges();
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='ep_courseinfo.aspx'", true);
            }
            catch (Exception)
            {

                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败');", true);
            }
        }
    }

    /// <summary>
    /// 获取实体对象
    /// </summary>
    /// <param name="m_epci"></param>
    /// <returns></returns>
    private EMPCModel.ePioneer_CourseInfo GetModel(EMPCModel.ePioneer_CourseInfo m_epci)
    {
        string imgPath = System.Configuration.ConfigurationManager.AppSettings["imgPath"].ToString();      //imgpath
        string filePath = System.Configuration.ConfigurationManager.AppSettings["filePath"].ToString();       //filepath

        m_epci.IsLine = Convert.ToInt32(rbl_Line.SelectedValue);
        m_epci.CourseType = ddl_CourseType.SelectedItem.Text.Trim();
        m_epci.SolutionType = ddl_SolutionType.SelectedItem.Text.Trim();
        m_epci.Lecturer = Convert.ToInt32(ddl_Lecturer.SelectedValue);
        m_epci.BriefInfo = txt_BriefInfo.Text.Trim();
        m_epci.EndTime = dll_EndTime.SelectedItem.Text;
        if (txt_IReservation.Text.Trim() != "")
        {
            m_epci.IReservation = Convert.ToInt32(txt_IReservation.Text.Trim());
        }
        m_epci.MainContent = txt_MainContent.Text.Trim();
        m_epci.StartTime = dll_StartTime.SelectedItem.Text;
        m_epci.Title = txt_Title.Text.Trim();
        m_epci.CourseStartDate = Convert.ToDateTime(txt_CourseStartDate.DateTimeValue.ToShortDateString() + " " + m_epci.StartTime);
        m_epci.Url = txt_url.Text.Trim();
        if (checkIsTop.Checked)
            m_epci.IsTop = 1;
        else
            m_epci.IsTop = 0;
        if (flImg.FileBytes.Length > 0)
        {
            var data = flImg.FileBytes;
            string msg = "";
            string path = filePath + "pioneer/" + flImg.FileName;
            string strImgPath = imgPath + "pioneer/" + flImg.FileName;
            m_epci.AttachmentName = flImg.FileName;
            m_epci.Attachment = strImgPath;
            Deliver.AsynchronousClient.StartTranCommand(data, path, out msg);
        }
        return m_epci;
    }

}